﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace P2P_Backup.tools.io
{
    static class FileTools
    {
        public static FileStream TryOpenFile(string path, FileAccess access)
        {
            try
            {
                if (!File.Exists(path)) return null;
                return File.Open(path, FileMode.Open, access);
            }
            catch (IOException) { return null; }
            catch (UnauthorizedAccessException) { return null; }
        }

        public static FileStream WaitAndOpenFile(string path, FileAccess access)
        {            
            FileStream fs = null;
            while ((fs = TryOpenFile(path, access)) == null)
            {
                Thread.Sleep(250);
            }
            return fs;
        }

    }
}
